package gridy.algorithm;

/**
 * Osobnik reprezentujący rozwiązanie funkcji Rastrigina
 * @author krzysiek
 *
 */
public class IndividualImpl implements Individual {
    
    private double genes[];
    private double[] boundries = new double[] { -5.12, 5.12 };
    private double fitness;
    private double value;
    
    public IndividualImpl(int numOfGenes) {
        genes = new double[numOfGenes];
    }

    public IndividualImpl(IndividualImpl org) {
        
        this.genes = new double[org.genes.length];
        for (int i = 0; i < org.genes.length; i++) {
            this.genes[i] = org.genes[i];
        }
    }

    @Override
    public double getGene(int i) {
        return genes[i];
    }
    
    public void setGene(int i, double value) {
        genes[i] = value;
    }

    @Override
    public Individual duplicate() {
        return new IndividualImpl(this);
    }

    @Override
    public double getBoundry(int i) {
        return boundries[i];
    }

    @Override
    public int getGenesNum() {
        return 10;
    }

    @Override
    public void normalizeGenes() {
        for (int i = 0; i < genes.length; i++) {
            
            if (genes[i] < -5.12) {
                genes[i] = -5.12;
            }
            if (genes[i] > 5.12) {
                genes[i] = 5.12;
            }
        }
        
    }

    public double getFitness() {
        return fitness;
    }

    public void setValue(double d) {
        this.value = d;
        this.fitness = 1.0 / (1.0 + d);
    }

    @Override
    public double getValue() {
        return this.value;
    }

    @Override
    public int compareTo(Individual o) {
        return (new Double(fitness)).compareTo(o.getFitness());
    }
}
